package com.jhx.mall.app.servlet;

import com.jhx.mall.app.service.DtsGoodsService;
import com.jhx.mall.app.service.DtsGoodsSpecificationService;
import com.jhx.mall.app.service.DtsIssueService;
import com.jhx.mall.app.service.impl.DtsGoodsServiceImpl;
import com.jhx.mall.app.service.impl.DtsGoodsSpecificationServiceImpl;
import com.jhx.mall.app.service.impl.DtsIssueServiceImpl;
import com.jhx.mall.entity.DtsGoods;
import com.jhx.mall.entity.DtsGoodsSpecification;
import com.jhx.mall.entity.DtsIssue;
import com.jhx.mall.util.BaseServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DtsGoodsDetail", value = "/api/index/goodsDetail")
public class DtsGoodsDetailServlet extends BaseServlet {
    private DtsIssueService issueService = null;

    private DtsGoodsService goodsService = null;

    private DtsGoodsSpecificationService goodsSpecificationService = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        issueService = new DtsIssueServiceImpl();
        goodsService = new DtsGoodsServiceImpl();
        goodsSpecificationService = new DtsGoodsSpecificationServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        if(opr.equals("issue")){ // 获取所有问题
            List<DtsIssue> list = issueService.selectIssueAll();
            if(list != null){
                ok(request,response,list);
            }else{
                error(request,response,list);
            }
        } else if (opr.equals("seeBoth")) { // 分类查询
            long id = Long.parseLong(request.getParameter("categoryId"));
            List<DtsGoods> list = goodsService.selectGoodsByCategoryId(id);
            if (list != null) {
                ok(request,response,list);
            } else {
                error(request,response,list);
            }
        } else if (opr.equals("specifiction")) {
            int id = Integer.parseInt(request.getParameter("goodsId"));
            List<DtsGoodsSpecification> list = goodsSpecificationService.selectGoodsSpecificationCountByGoodsId(id);

            List<DtsGoodsSpecification> goodsSpecificationList = goodsSpecificationService.selectGoodsSpecificationByGoodsId(id, list);
            if (goodsSpecificationList != null) {
                ok(request,response,goodsSpecificationList);
            } else {
                error(request,response,goodsSpecificationList);
            }
        } else if (opr.equals("addCart")) {

        }
    }
}
